<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<script type="text/javascript">
description('The closing handshake should start when all data is transmitted.');

window.jsTestIsAsync = true;

var ws = new WebSocket('ws://127.0.0.1:8880/count-received-bytes');

var message = 'a';
for (var i = 0; i < 16; ++i)
    message = message + message;

ws.onopen = function()
{
    ws.send(message);
    ws.send(message);
    ws.send(message);
    window.bufferedAmount = ws.bufferedAmount;
    shouldBe('bufferedAmount', '196608');
    ws.close();
};

ws.onclose = function(e)
{
    debug('onclose');
    window.bufferedAmount = ws.bufferedAmount;
    window.wasClean = e.wasClean;
    window.reason = e.reason
    shouldBe('bufferedAmount', '0');
    shouldBeTrue('wasClean');
    shouldBeEqualToString('reason', 'received 196608 bytes'); 
    finishJSTest();
};

</script>
</body>
</html>
